<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${dataId ? '编辑' : '新增'}-纱种工艺`"
		width="800px"
		:show-content-loading="showLoading"
		:save-audit-btn-loading="saveAuditLoading"
		:update-audit-btn-loading="updateAuditLoading"
		:save-btn-loading="saveBtnLoading"
		append-to-body
		:show-confirm-btn="false"
		:show-save-btn="true"
		:permission-prefix="permissionPrefix"
		:show-save-audit-btn="false"
		:show-update-audit-btn="false"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-save="onConfirmBtn('save')"
		@on-save-audit="onConfirmBtn('saveAndAudit')"
		@on-update-audit="onConfirmBtn('updateAndAudit')"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="110px" :show-message="false">
			<el-row ref="box1">
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="纱种编码" prop="code">
						<el-input v-model="ruleForm.code" placeholder="纱种编码" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="纱种名称" prop="name">
						<el-input v-model="ruleForm.name" placeholder="纱种名称" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="margin-bottom-10" label="项目">
						<jk-table :data="tableData" :height="dialogTableHeight - 20">
							<vxe-column title="操作" align="center" field="operation" :width="120">
								<template slot-scope="scope">
									<div class="flex-center">
										<div class="item-gap"><el-button size="mini" type="text" icon="el-icon-circle-plus" @click="onAddBtn(scope)"></el-button></div>
										<el-button size="mini" icon="el-icon-remove" type="text" @click="onRemoveBtn(scope)"></el-button>
									</div>
								</template>
							</vxe-column>
							<vxe-column title="编号" align="left" field="code" :min-width="100">
								<template slot-scope="scope">
									<jk-input v-model="scope.row.code" size="mini" placeholder=""></jk-input>
								</template>
							</vxe-column>
							<vxe-column title="名称" align="left" field="name" :min-width="100">
								<template slot-scope="scope">
									<jk-input v-model="scope.row.name" size="mini" placeholder=""></jk-input>
								</template>
							</vxe-column>
							<vxe-column title="排序" align="center" field="sortNum" :min-width="100">
								<template slot-scope="scope">
									<jk-number-input v-model="scope.row.sortNum" size="mini" placeholder=""></jk-number-input>
								</template>
							</vxe-column>
						</jk-table>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import { spinVarietyYarnSpecCreateAudit, spinVarietyYarnSpecUpdateAudit, spinVarietyYarnSpecCreate, spinVarietyYarnSpecUpdate, spinVarietyYarnSpecGet } from '../../../api/technology/yarnSeedTechnology';
    export default {
        name: 'TechnologyItem',
        mixins: [calHeight],
        props: {
            // 页面权限的前缀
            permissionPrefix: {
                type: String,
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        data() {
            return {
                tableData: [{}],
                tableLoading: false,
                updateAuditLoading: false,
                saveAuditLoading: false,
                saveBtnLoading: false,
                workshopList: [],
                showDialog: false,
                showLoading: true,
                confirmBtnLoading: false,
                saveDialogState: false,
                ruleForm: {
                    'code': '',
                    'name': '',
                    'status': false
                },
                rules: {
                    processId: [{ required: true, trigger: 'change' }],
                    code: [{ required: true, trigger: 'change' }],
                    name: [{ required: true, trigger: 'change' }]
                }
            };
        },
        methods: {
            onAddBtn(scope) {
                this.tableData.splice(scope.rowIndex + 1, 0, this.defaultRowData());
            },
            onRemoveBtn(scope) {
                if (this.tableData.length > 1) {
                    this.tableData.splice(scope.rowIndex, 1);
                }
            },
            defaultRowData() {
                return {
                    code: null,
                    name: '',
                    sortNum: null
                };
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                params.varietyYarnSpecItemList = this.tableData.map(x => x);
                return params;
            },
            saveAndAuditRequest() {
                this.saveAuditLoading = true;
                this.ruleForm.id = null;
                spinVarietyYarnSpecCreateAudit(this.getParamData()).then(res => {
                    this.saveAuditLoading = false;
                    if (res.data.status === 200) {
                        if (res.data.status === 200) {
                            this.$tipsBar(this, 'operation');
                            this.$emit('on-finish');
                        }
                    }
                });
            },
            onConfirmBtn(type) {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        if (type === 'save') {
                            this.createAndUpdateRequest();
                        } else if (type === 'saveAndAudit') {
                            this.saveAndAuditRequest();
                        } else if (type === 'updateAndAudit') {
                            this.updateAndAuditRequest();
                        }
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            createAndUpdateRequest() {
                this.saveBtnLoading = true;
                // 新增
                if (!this.dataId) {
                    this.ruleForm.id = null;
                    spinVarietyYarnSpecCreate(this.getParamData()).then(res => {
                        this.saveBtnLoading = false;
                        if (res.data.status === 200) {
                            this.$tipsBar(this, 'save');
                            this.$emit('on-finish');
                        }
                    });
                    return;
                }
                // 编辑
                spinVarietyYarnSpecUpdate(this.getParamData()).then(res => {
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-finish');
                    }
                });
            },
            updateAndAuditRequest() {
                this.updateAuditLoading = true;
                spinVarietyYarnSpecUpdateAudit(this.getParamData()).then(res => {
                    this.updateAuditLoading = false;
                    if (res.data.status === 200) {
                        if (res.data.status === 200) {
                            this.$tipsBar(this, 'operation');
                            this.$emit('on-finish');
                        }
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return spinVarietyYarnSpecGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        const resData = res.data.res ? JSON.parse(JSON.stringify(res.data.res)) : {};
                        this.ruleForm = resData;
                        this.tableData = resData.varietyYarnSpecItemList;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
<style scoped lang="scss">
.menu-title-icon {
    font-size: 29px;
    color: #909399;
    margin-right: 4px;
}
.edit-tab-icon {
    margin-left: 8px;
    font-size: 13px;
}
.edit-tab-icon:hover {
    cursor: pointer;
}
.drag-info-icon {
    font-size: 14px;
}
.drag-btn {
    cursor: move;
    color: #2d8cf0;
    transition: all .5s;
    font-size: 18px;
}
.drag-btn:hover {
    transform: rotate(90deg);
}
</style>
